package com.wzf.greattruth.aio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.util.concurrent.CountDownLatch;

/**
 * 
 * @author wuzhengfei
 *
 */
public class AsyncServerHandler implements Runnable {

	CountDownLatch latch;
	AsynchronousServerSocketChannel asynServerSocketChannel;

	public AsyncServerHandler(int port) {
		try {
			asynServerSocketChannel = AsynchronousServerSocketChannel.open();
			asynServerSocketChannel.bind(new InetSocketAddress(port));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {

		latch = new CountDownLatch(1);
		asynServerSocketChannel.accept(this, new ServerAcceptCompletionHandler());
		try {
			latch.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
